<?php
namespace app\api\service\v1;

/*
 * content : admin service 服务类 
 * Author  : Songyicong 
 * time    : 2018-04-28 15:57:41 
 * email   : m13726262373@163.com 
 */

use app\api\service\BaseService;
use app\api\model\v1\Admin as AdminModel;
use app\api\model\v1\AdminAuth as AdminAuthModel;
use app\api\model\v1\AdminOpend as AdminOpendModel;
use think\Db;
use app\api\extra\Utils;
use app\api\extra\Send;

class AdminService extends BaseService{

    /** 登录 */
    public function login($params = []){
        if(empty($params['login_name']) || empty($params['password']) || empty($params['type']))
            return self::sendError(100201, 'the default parameter cannot be empty',200);
        // 判断登录信息是否正确
        $user = AdminModel::getLoginUser($params['login_name']);
        if($user&& md5($params['password']) == $user['password']){
            // 开始判断密码是否正确
            $userAuth = AdminOpendModel::isAuth($user['id']);
            $data['update_time'] = time();
            $data['type'] = $params['type'];
            // 生成open_id
            $openid = Utils::getOpenId($params['access_token'],$user['id'],$user['auth_num']);
            $data['open_id'] = $openid;
            if(!$userAuth){
                $data['create_time'] = time();
                $data['admin_id'] = $user['id'];
                $data['ip'] = get_client_ip();
                $data['access_token'] = $params['access_token'];
                $data['auth_num'] = $user['auth_num'];
                $data['name'] = $user['user_name'];
                $aid = Db::name('admin_opind')->insertGetId($data);
            }else{
                $data['ip'] = get_client_ip();
                $data['access_token'] = $params['access_token'];
                $data['auth_num'] = $user['auth_num'];
                $data['name'] = $user['user_name'];
                $aid = Db::name('admin_opind')->where('admin_id',$user['id'])->update($data);
            }
            $return['openid'] = $openid;
            $return['auth_num'] = $user['auth_num'];
            return self::returnmsg(200, 'success', $return, []);
        } else{
            return self::sendError(100202, 'password is error',200);        
        }
        // 登录名错误
        return self::sendError(100202, 'login name is error',200); 
    }

    public function setUser($params = []){
        if(empty($params['lname']) || empty($params['uname']) || empty($params['password'])|| empty($params['state'])|| empty($params['auth_num'])|| empty($params['ips'])){
            return self::returnmsg(400, 'parameter cannot be empty', [], []);
        }
        // 判断用户名 或者 用户信息是否存在
        if(AdminModel::isLogin($params['lname'])){
            return self::returnmsg(400, 'login name already exists', [], []);
        }
        $data['create_time'] = time();
        $data['login_name'] = $params['lname'];
        $data['user_name'] = $params['uname'];
        $data['ip'] = $params['ips'];
        // 前端 一次md5
        $data['password'] = md5($params['password']);
        $secret = Utils::getSecret($data['password']);
        $client_id = Utils::getAppId($data['login_name']);
        $data['secret'] = $secret;
        $data['client_id'] = $client_id;
        $data['status'] = $params['state'];
        $data['encry_type'] = 2;
        $data['auth_num'] = $params['auth_num'];
        $data['sign'] = $params['auth_num'];
        $uid = Db::name('admin')->insertGetId($data);

        $adminInfo =[
            'uid' => $uid, 
            'app_secret' => $secret,
            'app_id' => $client_id, 
        ];
        return self::returnmsg(200, 'success', $adminInfo, []);
    }
}
